﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;

namespace Chaos.view.connect
{
    /// <summary>
    /// Interaction logic for connect.xaml
    /// </summary>
    public partial class connect : UserControl
    {
        public connect()
        {
            InitializeComponent();
            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(connect_IsVisibleChanged);

            view.manager m = FindResource("view_manager") as view.manager;
            if (m != null)
            {
                m.Controls.Add("connect_control", this);
            }

        }
        
        public void connect_OnAddServerInfo(connect.AddServerInfoEvent e)
        {
                 for (int i = 0; i < ListConnections.Items.Count; i++)
            {
                IPEndPoint ip = ListConnections.Items[i] as IPEndPoint;

                if (ip.Equals(e.IPEndPoint))
                {
                    return;
                }
            }
            ListConnections.Items.Add(e.IPEndPoint);
        }
        
        void connect_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.Visibility == Visibility.Visible )
            {
                network.detectionclient d = FindResource("detection_client") as network.detectionclient;
                if (d != null)
                {
                    d.start(network.detectionserver.Port);
                }
            }
            else
            {
                network.detectionclient d = FindResource("detection_client") as network.detectionclient;
                if (d != null)
                {
                    d.shutdown();
                }
            }
        }

        private void HostGame_MouseDown(object sender, MouseButtonEventArgs e)
        {
            HostGame.Foreground = new SolidColorBrush(Colors.Yellow);
        }

        private void HostGame_MouseEnter(object sender, MouseEventArgs e)
        {
            HostGame.Foreground = new SolidColorBrush(Colors.SkyBlue);
        }

        private void HostGame_MouseLeave(object sender, MouseEventArgs e)
        {
            HostGame.Foreground = new SolidColorBrush(Colors.White);
        }

        private void HostGame_MouseUp(object sender, MouseButtonEventArgs e)
        {
            HostGame.Foreground = new SolidColorBrush(Colors.White);
            commands.hostgame c = FindResource("command_hostgame") as commands.hostgame;
            if (c != null)
            {
                if (c.CanExecute(null))
                {
                    c.Execute(null);
                }
            }
        }

        private void ListConnections_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            commands.connect c = FindResource("command_connect") as commands.connect;
            if (c != null)
            {
                if (c.CanExecute(ListConnections.SelectedItem))
                {
                    c.Execute(ListConnections.SelectedItem);
                }
            }
        }

        public class AddServerInfoEvent : EventArgs
        {
            public IPEndPoint IPEndPoint { get; set; }
            public AddServerInfoEvent()
            {
            }
        };
        public delegate void OnAddServerInfoDelegate(AddServerInfoEvent e);
    }
}
